package com.xingkeduo.business.transformation.json;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.table.JobseekerJobDetailTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;

/**
 * 求职者工作经历封装类
 * 
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class JobseekerJobDetailJTO extends BaseJson {
	public static List<JobseekerJobDetailTO> createTableListForNew(String json) throws Throwable {
		List<JobseekerJobDetailTO> tables = new ArrayList<>();
		JsonNode root = validJson(json);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数格式");

		if (!root.has("details"))
			throw new InputParamException(InputParamException.Type.NULL, "工作经历");

		for (Iterator<JsonNode> iterator = root.path("details").elements(); iterator.hasNext();) {
			JsonNode detail = iterator.next();
			tables.add(createTable(detail));
		}

		return tables;
	}

	private static JobseekerJobDetailTO createTable(JsonNode node) throws Throwable {
		JobseekerJobDetailTO table = new JobseekerJobDetailTO();
		table.setStartTime(validStartTime(node, YesNoEnum.YES)).setEndTime(validEndTime(node, YesNoEnum.YES))
				.setCompany(validCompany(node, YesNoEnum.YES)).setLeaveReason(validLeaveReason(node, YesNoEnum.YES))
				.setProveName(validProvename(node, YesNoEnum.YES)).setProvePhone(validProvephone(node, YesNoEnum.YES));

		return table;
	}

	public static ObjectNode toJson(JobseekerJobDetailTO table) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();
		SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd");

		if (table != null) {
			if (table.getStartTime() != null)
				root.put("starttime", formatDateTime.format(table.getStartTime()));
			if (table.getEndTime() != null)
				root.put("endtime", formatDateTime.format(table.getEndTime()));
			if (table.getCompany() != null)
				root.put("company", table.getCompany());
			if (table.getLeaveReason() != null)
				root.put("leavereason", table.getLeaveReason());
			if (table.getProveName() != null)
				root.put("provename", table.getProveName());
			if (table.getProvePhone() != null)
				root.put("provephone", table.getProvePhone());

			toBaseJson(root, table);
		}

		return root;
	}

	public static ArrayNode toJson(List<JobseekerJobDetailTO> tables) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if (!CommonUtil.isEmpty(tables))
			for (Iterator<JobseekerJobDetailTO> iterator = tables.iterator(); iterator.hasNext();) {
				root.addPOJO(toJson(iterator.next()));
			}

		return root;
	}

	public static Date validStartTime(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "starttime";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isDateTime(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.parse(value);
		}
		return null;
	}

	public static Date validEndTime(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "endtime";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isDateTime(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.parse(value);
		}
		return null;
	}

	public static String validCompany(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "company";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (CommonUtil.isEmpty(value) || value.length() > 50)
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return value;
		}
		return null;
	}

	public static String validLeaveReason(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "leavereason";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (CommonUtil.isEmpty(value) || value.length() > 50)
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return value;
		}
		return null;
	}

	public static String validProvename(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "provename";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (CommonUtil.isEmpty(value) || value.length() > 50)
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return value;
		}
		return null;
	}

	public static String validProvephone(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "provephone";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isSelfPhone(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return value;
		}
		return null;
	}
}
